package com.zyh.controller;

import com.zyh.entity.User;
import com.zyh.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private ModelAndView mav;

    @RequestMapping("selectUserByUsernameAndPasswordMvc")
    public ModelAndView selectUserByUsernameAndPasswordMvc(@Param("checkCode") String checkCode,HttpSession httpSession, @Param("username") String username, @Param("password") String password){
        User user = userService.selectByUsernameAndPassword(username, password);
        String safeCode = (String)httpSession.getAttribute("checkCode");
        if(user!=null && safeCode.equals(checkCode)){
            httpSession.setAttribute("user",user);
            mav.setViewName("/book/selectAllByPageMvc.do");
        }else{
            mav.setViewName("redirect:/login.jsp");
        }
        return mav;
    }
    @RequestMapping("insertMvc")
    public ModelAndView insertMvc(User user){
        userService.insert(user);
        mav.setViewName("redirect:/login.jsp");
        return mav;
    }

}
